﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace milanov
{
    //Thom
    public partial class View : System.Web.UI.Page
    {
        private Guid UserId
        {
            get
            {
                return Cartclass.getUserID(User.Identity.Name);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //als er niets in de parameter staat transfer user naar home
            if (Request.QueryString["order"] != null)
            {
                int ordernr;
                bool intparameter = int.TryParse(Request.QueryString["order"], out ordernr);
                //wanneer het tryparsen is gelukt, laad de methode met parameter
                if (intparameter)
                {
                    Page.Title = "Orderoverzicht van " + User.Identity.Name;
                    OrdernrLabel.Text = "Ordernummer: " + ordernr.ToString();
                    //als hij geen items uit de db kan halen, user naar homepage
                    if (!loadOrderGrid(ordernr, UserId))
                    {
                        Server.Transfer("~/Cart.aspx");
                    } 
                }
                else
                {
                    Server.Transfer("~/Default.aspx");
                }
            }
        }
        public bool loadOrderGrid(int OrderID, Guid User)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["fotosDB"].ConnectionString))
            {

                string query = "SELECT f.* FROM Fotos f, Orders o, Cart c WHERE f.FileID = c.PhotoId AND c.OrderId = o.OrderId AND o.Completed = 1 AND o.UserId = @user AND c.OrderId = @orderid;";
                using (SqlDataAdapter da = new SqlDataAdapter(query, con))
                {
                    DataTable dt = new DataTable();
                    da.SelectCommand.Parameters.AddWithValue("@user", User);
                    da.SelectCommand.Parameters.AddWithValue("@orderid", OrderID);
                    da.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        return false;
                    }
                    else
                    CompletedGrid.DataSource = dt;
                    CompletedGrid.DataBind();
                    return true;
                }
            }
        }
    }
}